﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DoNet.FrameWork.Services.Impl;
using DoNet.FrameWork.Repository;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Repositories;

namespace Authority.Services.Impl
{
    public class OrganizeServiceImpl : ServicesBase<Organize>, IOrganizeService
    {
        public OrganizeServiceImpl(IUnitOfWork _context, IOrganizeRepository _Repository)
            : base(_context, _Repository)
        {

        }

        public string InsertOrEditOrganize(Organize entity)
        {
            //if (entity.ParentId == null) entity.ParentId = new Guid("00000000-0000-0000-0000-000000000000");
            ISpecification<Organize> condition = new DirectSpecification<Organize>(o => o.Id == entity.Id);
            Organize organzie = base.GetByCondition(condition);
            if (organzie != null)
            {
                organzie.Code = entity.Code;
                organzie.OrganizeName = entity.OrganizeName;
                organzie.ParentId = entity.ParentId;
                //organzie.SysCreatedBy = entity.SysCreatedBy;
                //organzie.SysCreatedOn = entity.SysCreatedOn;
                organzie.SysModifiedBy = entity.SysModifiedBy;
                organzie.SysModifiedOn = entity.SysModifiedOn;
                base.Modify(organzie);
            }
            else
            {
                base.NewSave(entity);
            }
            return "保存成功！";
        }

        public string DeleteOrganizeById(Guid id)
        {
            ISpecification<Organize> sonConditon = new DirectSpecification<Organize>(o => o.ParentId == id);
            ISpecification<Organize> condition = new DirectSpecification<Organize>(o => o.Id == id);
            condition = new OrSpecification<Organize>(condition, sonConditon);
            //Organize organize = base.GetByCondition(condition);
            base.Removes(condition);
            return "删除成功！";
        }
    }
}
